import React, {Component} from 'react'
import { withRouter } from 'react-router-dom';
import { Button, Tools,Modal, Bubble } from "weaver-mobile";
import '../../../style/qrLogin.less';
const getLabel = Tools.getLabel;
const {alert} = Modal ;

@withRouter
export default class QRCACheck extends Component {
  constructor(props) {
    super(props);
    this.state = {
      loginkey: '',
      supportca: '',
      operateflag: '',
      logindisable : true
    }
  }

  isCaOPen = false

  componentDidMount(){
    Tools.callApi({url:'/api/hrm/login/qrcode/isCaOPen'}).then( data => {
      this.isCaOPen=data.isCaOPen ;
      let search = this.props.location.search
      const obj = {};
      
      if(search.trim().length > 0){
        search = search.substring(1);
        const params = search.split('&');
        params.map(param => {
          const kv = param.split('=');
          Object.assign(obj, {
            [kv[0]]: kv[1]
          })
        })
      }
      this.setState({'logindisable':false,...obj});
      this.onConfirm();
    }).catch( res =>{})
  }

  onConfirm = () => {
    this.checkNeedCaAndInvoke(this.state,afterInvokeParams =>{
      Tools.callApi({url: '/api/hrm/login/saveCAAuth', method: 'POST', params: afterInvokeParams}).then(data => {
        // const loginStatus = data['loginStatus']
        // if('1' == loginStatus){
        //   this.onCancel();
        // }else{
        //   alert(getLabel('25700','错误信息'),'error:'+loginStatus+";msg:"+data['message'])
        // }
        
        
      }).catch((res) => {});
      setTimeout(() => {
        this.onCancel();
      }, 500);
    })
  }

  checkNeedCaAndInvoke=(params,callback)=>{
    let afterInvokeParams ={...params}
     if(this.isCaOPen){
       this.invokeCaSignValueNoPassForJsApi(afterInvokeParams,(signInfo)=>{
           const errCode = signInfo['errCode']
           const errMsg = signInfo['errMsg']
           if(errCode =='0'){
             //success
             const {certInfo} = signInfo
             afterInvokeParams['cert'] = certInfo['cert'] 
             
             afterInvokeParams['signAlg'] = certInfo['signAlg']
             afterInvokeParams['signatureValue'] = certInfo['signValue']
             afterInvokeParams['isSigned']='1'
             afterInvokeParams.authCode = params.uid || '';
             callback(afterInvokeParams)
           }else{
            Bubble.error(getLabel('25700','错误信息')+',error:'+errCode+';msg:'+errMsg);
            this.onCancel();
           }
       })
     }else{
         callback(afterInvokeParams)
     }
   }

   invokeCaSignValueNoPassForJsApi = (params,callback) => {
    const em = window.em ;
    // 1 check jsapi active
    if(!em){
      Bubble(getLabel('25700','错误信息'),'error:-900') ;
      return ;
    }
    
    if(em.CAMethod){
        em.CAMethod({
            func:'getSignValueNoPass',
            original:params['randomNumber'],
            success : callback,
            callback: 'certInfo'
        })
    }else{
      alert(getLabel('25700','错误信息'),'error:-902')
      return
    }
    
  }

  onCancel = () => {
    if(window.em){
      window.em.closeWindow();
    }
  }

  render() {
    return (
      <Modal
        visible={false}
        transparent
        maskClosable={false}
      >
      </Modal>
      // <div className='qrLogin'>
      //   <div className='title'>
      //     <div>
      //       <img src='/hrm/hrm_e9/image/pc.png' />
      //       <div className='tip'>{getLabel('390249',"PC版登录确认")}</div>
      //     </div>
      //   </div>
      //   <div className='btnGroup'>
      //     <Button disabled={this.state.logindisable} type='primary' className='login' onClick={this.onConfirm}>{getLabel('674',"登录")}</Button>
      //     <Button type='default' className='cancel' onClick={this.onCancel}>{getLabel('390291',"取消登录")}</Button>
      //   </div>
      // </div>
    )
  }
}